package com.li.service.impl;

import com.li.domain.UploadFile;
import com.li.mapper.UploadFileMapper;
import com.li.service.SystemAddressService;
import com.li.utlis.FilePathUtil;
import com.li.utlis.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.mapper.AdvertisementMapper;
import com.li.domain.Advertisement;
import com.li.service.AdvertisementService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementMapper, Advertisement> implements AdvertisementService{

    @Autowired
    private AdvertisementMapper advertisementMapper;

    @Autowired
    private SystemAddressService systemAddressService;

    @Autowired
    private UploadFileMapper uploadFileMapper;

    @Override
    public List<Advertisement> listPageAdvertisement(Advertisement advertisement, Integer page, Integer limit) {
        Integer indexPage=(page-1)*limit;
        return advertisementMapper.listPageAdvertisement(advertisement,indexPage,limit);
    }

    @Override
    public Integer listPageAdvertisementCount(Advertisement advertisement) {
        return advertisementMapper.listPageAdvertisementCount(advertisement);
    }

    /**
     * 上传文件
     * @param uploadFile
     * @param req
     * @return
     */
    @Transactional
    @Override
    public String uploadImage(MultipartFile uploadFile,String remark, HttpServletRequest req) {
        /*
        上传文件 并 将数据上传文件记录表
         */
        //找到要上传的地址
        String path= FilePathUtil.getSystemAddressServicePath(systemAddressService, "data-advertisement");
        //完善地址 加"/"
        path= FilePathUtil.pathLast(path);
        //把文件上传
        UploadFile fileData = FileUploadUtil.uploadPahtFileAndUploadFile(uploadFile, path, req);
        //添加到文件记录表中
        uploadFileMapper.insert(fileData);
        //添加到图片管理中
        Advertisement advertisement=new Advertisement();
        advertisement.setUuid(fileData.getUuid());
        advertisement.setFileName(fileData.getOldName());
        advertisement.setUrl(fileData.getPath());
        advertisement.setFilepath(fileData.getFilePath());
        advertisement.setCreateTime(new Date());
        advertisement.setRemark(remark);
        advertisementMapper.insert(advertisement);
        //将访问地址返回
        return fileData.getPath();
    }

    @Override
    public void delAdvertisements(Integer[] ids) {
        for (Integer id:ids){
            Advertisement advertisement = advertisementMapper.selectById(id);
            if (advertisement==null){
                continue;
            }
            String filePath=advertisement.getFilepath();
            File file=new File(filePath);
            //如果这个文件物理存在
            if (file.exists()){
                file.delete();//删除文件
            }
        }
        advertisementMapper.deleteBatchIds(Arrays.asList(ids));
    }


}
